package demo01;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.*;
class Ticket{
    private int count = 3;
    private Lock lock = new ReentrantLock();
    public void sale(){
        this.lock.lock();
        try {
            if (this.count > 0){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("【" + Thread.currentThread().getName() + "】买票,剩余票数:" + this.count-- + "张票");
            }else {
                System.out.println("【" + Thread.currentThread().getName() + "】买票,已无票数！");
            }
        }catch (Exception e){}
        finally {
            this.lock.unlock();
        }
    }
}
public class Reendemo1 {
    public static void main(String[] args) throws  Exception {
        Ticket ticket = new Ticket();
        for(int x = 0; x < 5; x++){
            new Thread(( ) -> {
                ticket.sale();
            },"售票员 - " + x).start();
        }
    }
}